package gcu.module.room.service;

import com.google.common.collect.Maps;
import gcu.common.context.SecurityContextHolder;
import gcu.module.build.dao.BuildDao;
import gcu.module.build.daomain.Build;
import gcu.module.owner.dao.OwnerDao;
import gcu.module.owner.daomain.Owner;
import gcu.module.room.dao.RoomDao;
import gcu.module.room.daomain.Room;
import gcu.util.Pager;
import gcu.util.SFormt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2016/9/26.
 */
@Service
public class RoomService {
      @Autowired
      private RoomDao roomDao;
      @Autowired
      private BuildDao buildDao;
      @Autowired
      private OwnerDao ownerDao;

      public void addRoom(Room room) {
            room.setCreateTime(SFormt.NOWDATE_y_M_dhms());
            room.setIsIn(0);
            roomDao.addRoom(room);
      }

      public void deleteRoom(int id) {
            roomDao.deleteRoom(id);
      }
      public void deleteRoom(List list) {
            roomDao.deleteRoom(list);
      }

      public void deleteRoomByBuild(String buildName) {
            roomDao.deleteRoomByBuild(buildName);
      }

      public void updateRoom(Room room) {
            roomDao.updateRoom(room);
      }


      public List<Room> queryRoomList(Room room, Pager pager) {
            return roomDao.queryBuildList(room, pager);
      }

      @Transactional(readOnly = true)
      public Map<String, Object> get(Integer id, Integer buildId) {
            Map<String, Object> map = Maps.newHashMap();
            Room room = new Room();
            if (id == null) {
                  map.put("room", room);
            } else {
                  room = roomDao.queryRoomById(id);
                  map.put("room", room);
            }
            if (room.getOwnerId() != null) {
                  Owner owner = ownerDao.queryOwnerById(room.getOwnerId());
                  room.setOwnerName(owner.getOwnerName());
            }
            if (buildId != null) {
                  Build build = buildDao.queryBuildById(buildId);
                  room.setBuildName(build.getBuildName());
                  map.put("room", room);
            }
            return map;
      }

      public Room queryRoomByNameAndBuildName(String buildName, String roomNum) {
            return roomDao.queryRoomByNameAndBuildName(buildName, roomNum);
      }
      public Room queryRoomById(int id) {
            return roomDao.queryRoomById(id);
      }
}
